/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ml.training;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author TUPEO
 */
public class TrainingHelper {

    public static final int POPULAR1 = 0;
    public static final int POPULAR2 = 1;
    public static final int PROBABILITY = 2;

    public static final int CONTINOUS_VALUE1 = 3;
    public static final int CONTINOUS_VALUE2 = 4;

    boolean solveOverfitting = false, fillMissingValue = false;
    int fillMissingValueType;
    int continousValueType;

    File datasetFile, outFile;
    XSSFSheet trainingSheet, validateSheet, fullDatasetSheet;
    double keyRate;
    Document document;

    int deep = 0;

    public TrainingHelper(boolean solveOverfitting, boolean fillMissingValue, int fillMissingValueType, int continousValueType, File datasetFile, File outFile) throws FileNotFoundException, IOException {
        this.solveOverfitting = solveOverfitting;
        this.fillMissingValue = fillMissingValue;
        this.fillMissingValueType = fillMissingValueType;
        this.continousValueType = continousValueType;
        this.datasetFile = datasetFile;
        this.outFile = outFile;

        FileInputStream inFile = new FileInputStream(datasetFile);
        XSSFWorkbook readWorkbook = new XSSFWorkbook(inFile);
        trainingSheet = readWorkbook.getSheet("training");
        validateSheet = readWorkbook.getSheet("validation");
        fullDatasetSheet = readWorkbook.getSheet("full");

        document = new Document();

        if (continousValueType == CONTINOUS_VALUE1) {
            List trainingSet = new ArrayList();
            for (int i = 0; i < trainingSheet.getPhysicalNumberOfRows(); i++) {
                trainingSet.add(i);
            }

            Map bestRate = bestRate(trainingSet);
            keyRate = (double) bestRate.keySet().toArray()[0];
        }

    }

    private boolean optimalTree(Node root) {
        List<Node> parentOfLeafNode = new ArrayList<Node>();
        findParentOfLeafNode(root, parentOfLeafNode,0);
        if (parentOfLeafNode.size() > 0) {
            Iterator<Node> iterator = parentOfLeafNode.iterator();
            int size = parentOfLeafNode.size();
            while (iterator.hasNext()) {
                Node node = iterator.next();
                node.setCheckOptimal(true);
                float x1=accuracy(root); 
                float x2= accuracy2(root);
                System.out.println("x1="+x1+" x2="+x2);
                if (x1 <= x2) {
                    node.nodeId = Node.LEAF;
                    node.branch.clear();
                    node.branch.put("value", node.majorityClass);
                    node.setCheckOptimal(false);
                    size--;
                } else {
                    node.setCheckOptimal(false);
                }
            }
            if (size < parentOfLeafNode.size()) {
                return true;
            }
        }

        return false;
    }

    private void findParentOfLeafNode(Node node, List<Node> listParentOfLeftNode, int flag) {
        if (node.nodeId == Node.LEAF) {
            return;
        }
        Iterator it = node.branch.keySet().iterator();
        while (it.hasNext()) {
            Node n = (Node) node.branch.get(it.next());
            if (n.nodeId == Node.LEAF ) {
                if(!listParentOfLeftNode.contains(node) && flag!=0)
                listParentOfLeftNode.add(node);
            } else {
                findParentOfLeafNode(n, listParentOfLeftNode,1);
            }
        }

    }

    private float accuracy(Node node) {
        float numOfRightRow = 0;
        Iterator<Row> rows = validateSheet.iterator();
        while (rows.hasNext()) {
            Row testRow = rows.next();
            Node n = node;
//            System.out.println("row"+testRow.getRowNum());
            while (n.nodeId != Node.LEAF) {
                int at = n.nodeId;
                if (at != 3) {
                    String s = testRow.getCell(at).getStringCellValue().toLowerCase();
                    n = (Node) n.branch.get(s);
//                    System.out.println(at+" "+s);
                } else {
                    double d = testRow.getCell(at).getNumericCellValue();
                    if (d <= keyRate) {
                        n = (Node) n.branch.get("less");
                    } else {
                        n = (Node) n.branch.get("higher");
                    }
//                    System.out.println(at + " " + d);
                }

            }
            double x = (double) n.branch.get("value");
            if (x == testRow.getCell(13).getNumericCellValue()) {
                numOfRightRow++;
            }
//            System.out.println(x+"------------------------"+numOfRightRow);
        }
        return numOfRightRow / validateSheet.getPhysicalNumberOfRows();
    }

    private float accuracy2(Node node) {
        float numOfRightRow = 0;
        Iterator<Row> rows = validateSheet.iterator();
        while (rows.hasNext()) {
            Row testRow = rows.next();
            Node n = node;

            while (!n.getCheckOptimal()  &&  n.nodeId!=Node.LEAF) {
                int at = n.nodeId;
                if (at != 3) {
//                    System.out.println("row " + testRow.getRowNum() + "column " + at);
                    String s = testRow.getCell(at).getStringCellValue().toLowerCase();
                    n = (Node) n.branch.get(s);
//                    System.out.println(at+" "+s);
                } else {
                    double d = testRow.getCell(at).getNumericCellValue();
                    if (d <= keyRate) {
                        n = (Node) n.branch.get("less");
                    } else {
                        n = (Node) n.branch.get("higher");
                    }
//                    System.out.println(at+" "+d);
                }

            }
            double x = n.majorityClass;
            if (x == testRow.getCell(13).getNumericCellValue()) {
                numOfRightRow++;
            }
//            System.out.println(x+"------------------------"+numOfRightRow);
        }
        return numOfRightRow / validateSheet.getPhysicalNumberOfRows();
    }

    public void saveToFile() throws FileNotFoundException, IOException {
        List trainingSet = new ArrayList();
        List attributes = new ArrayList();
        for (int i = 0; i < trainingSheet.getPhysicalNumberOfRows(); i++) {
            trainingSet.add(i);
        }
        for (int i = 1; i < 13; i++) {
            attributes.add(i);
        }
        Node node = train(trainingSet, attributes, attributes);
        if (solveOverfitting) {
            while (optimalTree(node)) {
                System.out.println("optimal---");
            }
        }
//        System.out.println("accuracy="+accuracy(node));
        addNode(node, null, Double.toString(keyRate));
        //JDOM document is ready now, lets write it to file now
        XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
        //xmlOutputter.output(doc, System.out);
        xmlOutputter.output(document, new FileOutputStream(outFile));
    }

    public void addNode(Node node, Element parentElement, String parentValue) {

        if (parentElement == null) {
            Element root = new Element(fullDatasetSheet.getRow(0).getCell(node.nodeId).getStringCellValue().toLowerCase());
            root.setAttribute("keyrate", parentValue);
            document.setRootElement(root);

            Iterator it = node.branch.keySet().iterator();
            while (it.hasNext()) {
                String value = (String) it.next();
                Node childNode = (Node) node.branch.get(value);
                addNode(childNode, root, value);
            }

        } else {
            if (node.nodeId == Node.LEAF) {
                Element leaf = new Element("leaf");
                leaf.setText(node.branch.get("value").toString());
                leaf.setAttribute("parent", parentValue);
                parentElement.addContent(leaf);
            } else {
                Element element = new Element(fullDatasetSheet.getRow(0).getCell(node.nodeId).getStringCellValue().toLowerCase());
                element.setAttribute("parent", parentValue);
                parentElement.addContent(element);

                Iterator it = node.branch.keySet().iterator();
                while (it.hasNext()) {
                    String value = (String) it.next();
                    Node childNode = (Node) node.branch.get(value);
                    addNode(childNode, element, value);
                }
            }
        }

    }

    public Node train(List trainingSet, List classLabels, List attributes) {
        
        Node node = null;
        if (checkDatasetInSameClass(trainingSet)) {
            Map map = new HashMap();
            map.put("value", trainingSheet.getRow((int) trainingSet.get(0)).getCell(13).getNumericCellValue());
            node = new Node(Node.LEAF, map);
            return node;
        }
        if (attributes.isEmpty()) {
            Map map = new HashMap();
            map.put("value", majorityLabel(trainingSet));
            node = new Node(Node.LEAF, map);
            return node;
        }
        if(attributes.contains(0)) System.out.println("0 attribute");
        int bestAttribute = findBestAttribute(attributes, trainingSet);
//        System.out.println(parent+"="+valueOfParent+"=>"+bestAttribute);
//        System.out.println(bestAttribute);
        if (bestAttribute == 3) {
            Map mapRoot = new HashMap();
            Node left, right;
            List less = new ArrayList();
            List higher = new ArrayList();
            for (Object o : trainingSet) {
                Cell c = trainingSheet.getRow((int) o).getCell(3);
                if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                    continue;
                }
                if (c.getNumericCellValue() <= keyRate) {
                    less.add(o);
                } else {
                    higher.add(o);
                }
            }
            if (less.isEmpty()) {
                Map map = new HashMap();
                map.put("value", majorityLabel(trainingSet));
                left = new Node(Node.LEAF, map);
                mapRoot.put("less", left);

            } else {
                List attributesV = new ArrayList();
                for (Object o : attributes) {
                    if ((int) o != 3) {
                        attributesV.add(o);
                    }
                }
                left = train(less, classLabels, attributesV);
                mapRoot.put("less", left);
            }
            if (higher.isEmpty()) {
                Map map = new HashMap();
                map.put("value", majorityLabel(trainingSet));
                right = new Node(Node.LEAF, map);
                mapRoot.put("higher", right);
            } else {
                List attributesV = new ArrayList();
                for (Object o : attributes) {
                    if ((int) o != 3) {
                        attributesV.add(o);
                    }
                }
                System.out.println("trainingset"+less.size());
                right = train(higher, classLabels, attributesV);
                mapRoot.put("higher", right);
            }
            node = new Node(3, mapRoot, majorityLabel(trainingSet));
            return node;
        } else {
            Map mapRoot = new HashMap();
//            System.out.println("bestvalue="+bestAttribute);
            for (Object value : valuesOf(bestAttribute)) {
                Node childNode;
                List setOfValue = new ArrayList();
                for (Object o : trainingSet) {
                    Cell c = trainingSheet.getRow((int) o).getCell(bestAttribute);
                    if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                        continue;
                    }
                    if (c.getStringCellValue().toLowerCase().equals(value)) {
                        setOfValue.add(o);
                    }
                }
//                if(parent==11 && bestAttribute==1 && valueOfParent.equals("sequined")){
//                    System.out.println("values "+value);
//                    for(Object o:setOfValue){
//                        System.out.println(o);
//                    }
//                   
//                    System.out.println("----------------------");
//                }

                if (setOfValue.isEmpty()) {
//                    System.out.println(value+" is empty in "+ bestAttribute);
                    Map map = new HashMap();
                    map.put("value", majorityLabel(trainingSet));
                    childNode = new Node(Node.LEAF, map);
                    mapRoot.put((String) value, childNode);
                } else {
                    List attributesV = new ArrayList();
                    for (Object o : attributes) {
                        if ((int) o != bestAttribute) {
                            attributesV.add(o);
                        }
                    }
                    
                    childNode = train(setOfValue, classLabels, attributesV);
                    mapRoot.put((String) value, childNode);
                }

            }
            node = new Node(bestAttribute, mapRoot, majorityLabel(trainingSet));
            return node;
        }
    }

    private int findBestAttribute(List attribute, List trainingSet) {
        int t = 0;
        double gain = -1;
        int x=0;
        if(attribute.size()==1) return (int)attribute.get(0);
        for (Object o : attribute) {
            int i = (int) o;
            double g;
            if (i == 3) {
                g = gainOfRate(keyRate, trainingSet);
                if (gain < g) {
                    gain = g;
                    t = 3;
                    x++;
                }
            } else {
                g = gain(i, trainingSet);

                if (gain < g) {
                    gain = g;
                    t = i;
                    x++;
                }
                
            }
        }
        return t;
    }

    private List valuesOf(int attribute) {
        List list = new ArrayList();
        for (int i = 1; i <= 500; i++) {
            Cell c = fullDatasetSheet.getRow(i).getCell(attribute);
            if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                continue;
            }        
            String str = c.getStringCellValue();
            String lstr = str.toLowerCase();
            if (!str.isEmpty()) {
                if (!list.contains(lstr)) {
                    list.add(lstr);
                }
            }
        }
        return list;
    }

    private List valuesOf(int attribute, List trainingSet) {
        List list = new ArrayList();
        for (Iterator it = trainingSet.iterator(); it.hasNext();) {
            Object o = it.next();
            Cell c = trainingSheet.getRow((int) o).getCell(attribute);
            if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                continue;
            }
            String str = c.getStringCellValue();
            String lstr = str.toLowerCase();
            if (!str.isEmpty()) {
                if (!list.contains(lstr)) {
                    list.add(lstr);
                }
            }
        }
        return list;
    }

    private Map bestRate(List trainingSet) {
        double x = entropy(trainingSet);

        double gain = 0, bestRate = 0;
        List lessThanRate, higherThanRate;
        lessThanRate = new ArrayList();
        higherThanRate = new ArrayList();

        for (double i = 0; i <= 5; i += 0.1) {
            double numOfBlankCell = 0;
            i = (double) Math.round(i * 10) / 10;
            for (Object o : trainingSet) {
                Cell c = trainingSheet.getRow((int) o).getCell(3);
                if (c == null) {
                }
                if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                    if (fillMissingValue) {
                        switch (fillMissingValueType) {
                            case POPULAR1:
                                if ((double) fillEmptyCellType1((int) o, 3, trainingSet) <= i) {
                                    lessThanRate.add(o);
                                } else {
                                    higherThanRate.add(o);
                                }
                                break;
                            case POPULAR2:
                                if ((double) fillEmptyCellType2((int) o, 3, trainingSet) <= i) {
                                    lessThanRate.add(o);
                                } else {
                                    higherThanRate.add(o);
                                }
                                break;
                            case PROBABILITY:
                                lessThanRate.add(o);
                                higherThanRate.add(o);
                                numOfBlankCell++;
                                break;
                        }
                    }
                } else if (c.getCellType() != Cell.CELL_TYPE_BLANK) {
                    if (c.getNumericCellValue() <= i) {
                        lessThanRate.add(o);
                    } else {
                        higherThanRate.add(o);
                    }

                }
            }
            double h = (lessThanRate.size() - numOfBlankCell) / (trainingSet.size() - numOfBlankCell);
            double k = (higherThanRate.size() - numOfBlankCell) / (trainingSet.size() - numOfBlankCell);

            double igain = x - (lessThanRate.size() - (1 - h) * numOfBlankCell) / trainingSet.size() * entropy(lessThanRate) - (higherThanRate.size() - (1 - k) * numOfBlankCell) / trainingSet.size() * entropy(higherThanRate);
//            System.out.println(i+"="+igain+" "+lessThanRate.size()+"  "+higherThanRate.size());
//            if(i==4.3) System.out.println(igain);
            if (gain < igain) {
                gain = igain;
                bestRate = i;
            };
            lessThanRate.clear();
            higherThanRate.clear();
        }
        Map bestRateMap = new HashMap();
        bestRateMap.put(bestRate, gain);
        return bestRateMap;
    }

    private double gainOfRate(double keyValue, List trainingSet) {
        double x = entropy(trainingSet);
        List lessThanRate, higherThanRate;
        lessThanRate = new ArrayList();
        higherThanRate = new ArrayList();
        double numOfBlankCell = 0;

        for (Object o : trainingSet) {
            Cell c = trainingSheet.getRow((int) o).getCell(3);
            if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                if (fillMissingValue) {
                    switch (fillMissingValueType) {
                        case POPULAR1:
                            if ((double) fillEmptyCellType1((int) o, 3, trainingSet) <= keyValue) {
                                lessThanRate.add(o);
                            } else {
                                higherThanRate.add(o);
                            }
                            break;
                        case POPULAR2:
                            if ((double) fillEmptyCellType2((int) o, 3, trainingSet) <= keyValue) {
                                lessThanRate.add(o);
                            } else {
                                higherThanRate.add(o);
                            }
                            break;
                        case PROBABILITY:
                            lessThanRate.add(o);
                            higherThanRate.add(o);
                            numOfBlankCell++;
                            break;
                    }
                }
            } else if (c.getCellType() != Cell.CELL_TYPE_BLANK) {
                if (c.getNumericCellValue() <= keyValue) {
                    lessThanRate.add(o);
                } else {
                    higherThanRate.add(o);
                }
            }

        }
        double h = (lessThanRate.size() - numOfBlankCell) / (trainingSet.size() - numOfBlankCell);
        double k = (higherThanRate.size() - numOfBlankCell) / (trainingSet.size() - numOfBlankCell);
//        System.out.println("h="+h+" k="+k+" n="+numOfBlankCell+ "  less="+lessThanRate.size()+" hight="+higherThanRate.size());
        double igain = x - (lessThanRate.size() - (1 - h) * numOfBlankCell) / trainingSet.size() * entropy(lessThanRate) - (higherThanRate.size() - (1 - k) * numOfBlankCell) / trainingSet.size() * entropy(higherThanRate);
        return igain;
    }

    private double gain(int attribute, List trainingSet) {
        if (attribute == 3) {
            return gainOfRate(keyRate, trainingSet);
        }
        double x = entropy(trainingSet);
        for (Iterator it = valuesOf(attribute, trainingSet).iterator(); it.hasNext();) {
            Object value = it.next();
            List setOfThisValue = new ArrayList();
            double numOfBlankCell = 0;
            for (Iterator it1 = trainingSet.iterator(); it1.hasNext();) {
                Object rowId = it1.next();
                Cell c = trainingSheet.getRow((int) rowId).getCell(attribute);
                if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                    if (fillMissingValue) {
                        //fill empty value
                        switch (fillMissingValueType) {
                            case TrainingHelper.POPULAR1:
                                if (fillEmptyCellType1((int) rowId, attribute, trainingSet).equals(value)) {
                                    setOfThisValue.add(rowId);
                                }
                                break;
                            case TrainingHelper.POPULAR2:
                                if (fillEmptyCellType2((int) rowId, attribute, trainingSet).equals(value)) {
                                    setOfThisValue.add(rowId);
                                }
                                break;
                            case TrainingHelper.PROBABILITY:
                                setOfThisValue.add(rowId);
                                numOfBlankCell++;
                                break;
                        }
                    }

                } else if (c.getStringCellValue().toLowerCase().equals(value)) {
                    setOfThisValue.add(rowId);
                }
            }
            double y = (setOfThisValue.size() - numOfBlankCell) / (trainingSet.size() - numOfBlankCell);
            x -= ((setOfThisValue.size() - numOfBlankCell * (1 - y)) / trainingSet.size()) * entropy(setOfThisValue);
        }
        return x;
    }

    private Object fillEmptyCellType1(int x, int y, List trainingSet) {
        Object str = null;
        int i = 0;
        if (y == 3) {
            for (double j = 0; j <= 5; j += 0.1) {
                j = (double) Math.round(j * 10) / 10;
                List l = new ArrayList();
                for (Object row : trainingSet) {
                    Cell c = trainingSheet.getRow((int) row).getCell(y);
                    if (c != null && c.getCellType() != Cell.CELL_TYPE_BLANK) {
                        if (c.getNumericCellValue() == j) {
                            l.add(row);
                        }
                    }
                }
                if (l.size() > i) {
                    i = l.size();
                    str = j;
                }
            }
        } else {
            for (Object value : valuesOf(y, trainingSet)) {
                List l = new ArrayList();
                for (Object row : trainingSet) {
                    Cell c = trainingSheet.getRow((int) row).getCell(y);
                    if (c != null && c.getCellType() != Cell.CELL_TYPE_BLANK) {
                        if (c.getStringCellValue().toLowerCase().equals(value)) {
                            l.add(row);
                        }
                    }
                }
                if (l.size() > i) {
                    i = l.size();
                    str = value;
                }

            }
        }
        return str;
    }

    private Object fillEmptyCellType2(int x, int y, List trainingSet) {
        Object str = null;
        int i = 0;
        double classOfCell = trainingSheet.getRow(x).getCell(13).getNumericCellValue();
        if (y == 3) {
            for (double j = 0; j <= 5; j += 0.1) {
                j = (double) Math.round(j * 10) / 10;
                List l = new ArrayList();
                for (Object row : trainingSet) {
                    if (trainingSheet.getRow((int) row).getCell(13).getNumericCellValue() == classOfCell) {
                        Cell c = trainingSheet.getRow((int) row).getCell(y);
                        if (c != null && c.getCellType() != Cell.CELL_TYPE_BLANK) {
                            if (c.getNumericCellValue() == j) {
                                l.add(row);
                            }
                        }
                    }
                }
                if (l.size() > i) {
                    i = l.size();
                    str = j;
                }
            }
        } else {
            for (Object value : valuesOf(y, trainingSet)) {
                List l = new ArrayList();
                for (Object row : trainingSet) {
                    if (trainingSheet.getRow((int) row).getCell(13).getNumericCellValue() == classOfCell) {
                        Cell c = trainingSheet.getRow((int) row).getCell(y);
                        if (c!=null && c.getCellType() != Cell.CELL_TYPE_BLANK) {
                            if (c.getStringCellValue().toLowerCase().equals(value)) {
                                l.add(row);
                            }
                        }
                    }
                }
                if (l.size() > i) {
                    i = l.size();
                    str = value;
                }
            }
        }
        return str;
    }

    //ok
    private double entropy(List set) {
        double numOf1 = 0, numOf0 = 0;
        for (Object o : set) {
//            System.out.println(o);
            if (trainingSheet.getRow((int) o).getCell(13).getNumericCellValue() == 1) {
                numOf1++;
            } else {
                numOf0++;
            }
        }
        double p1 = numOf1 / (numOf0 + numOf1);
        double p0 = numOf0 / (numOf0 + numOf1);
        double x1, x0;
        if (p1 == 0) {
            x1 = 0;
        } else {
            x1 = -p1 * (Math.log(p1) / Math.log(2));
        }
        if (p0 == 0) {
            x0 = 0;
        } else {
            x0 = -p0 * (Math.log(p0) / Math.log(2));
        }
        return x0 + x1;

    }

    private double majorityLabel(List trainingSet) {
        int numOf1 = 0, numOf0 = 0;
        for (Object o : trainingSet) {
            if (trainingSheet.getRow((int) o).getCell(13).getNumericCellValue() == 1) {
                numOf1++;
            } else {
                numOf0++;
            }
        }
        if (numOf1 >= numOf0) {
            return 1;
        }
        return 0;

    }

    private boolean checkDatasetInSameClass(List trainingSet) {
        
        double d = trainingSheet.getRow((int) trainingSet.get(0)).getCell(13).getNumericCellValue();
        for (Iterator it = trainingSet.iterator(); it.hasNext();) {
            Object object = it.next();
            if (trainingSheet.getRow((int) object).getCell(13).getNumericCellValue() != d) {
                return false;
            }
        }
        return true;
    }

    private void fillMissingValue(File datasetFile) {

    }

    public void seeRating(File datasetFile) {
        try {
            FileInputStream inFile = new FileInputStream(datasetFile);
            XSSFWorkbook readWorkbook = new XSSFWorkbook(inFile);
            XSSFSheet readSheet = readWorkbook.getSheet("training");

            float[] rates = new float[51];
            for (float i : rates) {
                i = 0;
            }
            for (float i = 0; i <= 5; i += 0.1f) {
                int countR = 0, countN = 0, countRt = 0, countNt = 0;
                i = (float) Math.round(i * 10) / 10;
                Iterator<Row> rowIterator = readSheet.iterator();
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    if (row.getRowNum() == 0) {
                        continue;
                    }
                    if ((float) row.getCell(3).getNumericCellValue() < i + 0.05f) {
                        if (row.getCell(13).getNumericCellValue() == 1) {
                            countR++;
                        } else {
                            countN++;
                        }
                    } else {
                        if (row.getCell(13).getNumericCellValue() == 1) {
                            countRt++;
                        } else {
                            countNt++;
                        }
                    }

                }
                System.out.println(i + "=" + countR + "-" + countN + "|--|" + countRt + "-" + countNt);
            }

        } catch (IOException e) {

        }
    }
}
